/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom;

import ru.jSkyRoom.factory.ModFactory;
import java.util.HashSet;
import java.util.Set;
import ru.jSkyRoom.factory.BridgeFactory;
import ru.jSkyRoom.interfaces.JSkyMod;
import skyproc.Mod;
import skyproc.ModListing;
import skyproc.SPDatabase;

/**
 *
 * @author Rakev
 */
public class SyntaticModBuilder {

    public Set<JSkyMod> includingMods = new HashSet<>();

    public SyntaticModBuilder add(String... modNames) throws IllegalStateException {
        for (String modName : modNames) {
            add(modName);
        }
        return this;
    }

    public SyntaticModBuilder add(JSkyMod... mods) {
        for (JSkyMod mod : mods) {
            add(mod);
        }
        return this;
    }

    public SyntaticModBuilder add(String modName) throws IllegalStateException {
        ModListing modListing = new ModListing(modName);
        Mod mod = SPDatabase.getMod(modListing);
        if (mod == null) {
            throw new IllegalStateException("Mod not founded");
        }
        JSkyMod jsMod = getBridgeFactory().createBridge(mod);
        add(jsMod);
        return this;
    }

    public SyntaticModBuilder add(JSkyMod mod) {
        includingMods.add(mod);
        return this;
    }

    public JSkyMod build() {
        return build(createRandomName());
    }

    public JSkyMod build(String syntaticModName) {
        JSkyMod syntaticMod = getModFactory().create(syntaticModName, false);
        for (JSkyMod mod : includingMods) {
            syntaticMod.addAsOverrides(mod);
        }
        return syntaticMod;
    }

    private String createRandomName() {
        return "" + hashCode() + (int) (Math.random() * 1000000);
    }

    private BridgeFactory getBridgeFactory() {
        return new BridgeFactory();
    }

    private ModFactory getModFactory() {
        return new ModFactory();
    }
}
